package com.game77.guessTheWords2.util.offer;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;

import com.game77.guessTheWords2.MainActivity;
import com.game77.guessTheWords2.core.StageManager;
import com.game77.guessTheWords2.scene.ToolShopScene;
import com.juzi.main.AppConnect;

public class JuziOffer implements IOffer {
	String gold = "";
	ProgressDialog pd; // 进度条
	Dialog dialog; // 对话框

	@Override
	public void init(Context context) {
		AppConnect.getInstance(context);

	}

	@Override
	public void dispose(Context context) {
		AppConnect.getInstance(context).finalize();

	}

	@Override
	public void showOffer(Context context) {
		AppConnect.getInstance(context).showOffers(context);

	}

	@Override
	public void updateCoins(final Context context) {
		MainActivity.instance.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// 获取用户余额
				gold = AppConnect.getInstance(context).getPoints(context);
				if (!gold.contains("Error")) {
					// 花费用户虚拟货币
					int goldInt = Integer.parseInt(gold);
					if (goldInt > 0) {
						String str = AppConnect.getInstance(context)
								.spendPoints(goldInt, context);
						Toast.makeText(context, "成功获得" + gold + "个金币", 1000)
								.show();
						StageManager.instance
								.setGoldCount(StageManager.instance
										.getGoldCount() + (int) goldInt);
						ToolShopScene.make().updateCoins();
					}
				} else {
					dialog = new AlertDialog.Builder(MainActivity.instance)
							.setMessage(gold)
							.setPositiveButton("确定",
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface arg0, int arg1) {
											dialog.cancel();
										}
									}).show();
				}

			}
		});

	}
}
